﻿using B.S.Sterilizationcenter.Domain.yjb;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Writer.API.Appliaction.Commends.Yjb.ProgramInf;
using MediatR;
using SqlSugar;

namespace B.S.Sterilizationcenter.Writer.API.Appliaction.CommandHandlers.Yjb.ProgramInf
{
    public class DelProgramInfoCommandHandlers : IRequestHandler<DelProgramInfoCommand, ApiResult<int>>
    {
        private readonly ISqlSugarClient db;

        public DelProgramInfoCommandHandlers(ISqlSugarClient db)
        {
            this.db = db;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(DelProgramInfoCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<int> result = new ApiResult<int>();

                var del = await db.Queryable<ProgramInfo>().FirstAsync(p => p.ProgramInfoId == request.ProgramInfoId);
                del.IsDElete = true;
                var res = await db.Updateable(del).ExecuteCommandAsync();
                if (res > 0)
                {
                    result.Code = ApiEnums.删除成功;
                    result.Msg = "删除成功";
                }
                else
                {
                    result.Code = ApiEnums.删除失败;
                    result.Msg = "删除失败";
                }
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
